Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

303
Views
Why it is not an error to increment array "a" in the below function?
#include<stdio.h>
void printd(char []);
int main(void){
       char a[100];
       a[0]='a';a[1]='b';a[2]='c';a[4]='d';
       printd(a);
       return 0;
}
void printd(char a[]){
        a++;
        printf("%c",*a);
        a++;
        printf("%c",*a);
}

Explanation: I was expecting that it would result in lvalue error. But it is working with out any error and giving bc as output. Why is this incrementing array "a" is not an error?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If an array is passed to a function it decays to a pointer to the array's first element.

Due to this inside printd() the pointer a can be incremented and decremented, to point to different elements of the array a as defined in main().

Please note that when declaring/defining a function's parameter list for any type T the expression T[] is equivaltent to T*.

In question's specific case

void printd(char a[]);

is the same as

void printd(char * a);

The code below shows equivalent behaviour as the OP's code, with pa behaving like a in side printd():

#include <stdio.h>

int main(void)
{
   char a[100];
   a[0]='a';a[1]='b';a[2]='c';a[4]='d';

   {
     char * pa = a;

     pa++;
     printf("%c", *pa);
     pa++;
     printf("%c", *pa);
   }

   return 0;
`}
over 4 years ago · Santiago Trujillo Report

0

In C language array declaration in function parameter list and array declaration outside of function parameter list mean completely different things, even though they look similar (or the same) on the surface.

When you use array declaration in function parameter list (as is the case with void printd(char a[]) in your code), you are not declaring an array. The top-level [] syntax in function parameter list is just an alternative form of pointer declaration. This means that your a parameter is actually declared as char *a. It is not an array at all, it is an ordinary pointer. There's nothing unusual in being able to increment such a, and this is why you are not getting any "lvalue errors" from it.

Meanwhile, your a in main is a true array.

over 4 years ago · Santiago Trujillo Report

0

Why is this incrementing array "a" is not an error?

In C arrays are passed as a pointer to any function. Thats why you get no error.

The function calls in main() pass the name of the array, a, as an argument because the name of an array in an expression evaluates to a pointer to the array. In other words, the expression, a, is a pointer to (the first element of) the array, a[]. Its type is, therefore, char *, and a called function uses this pointer (passed as an argument) to indirectly access the elements of the array.

Now as you receive the address of the first element of a, a++ means ahead the array's initial address by 1. That's why the first printf prints b, the second element of array a.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!